home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 6 / FM Towns Free Software Collection 6.iso / t_os / book / src / mk.c < prev    next >
C/C++ Source or Header  |  1993-07-08  |  3KB  |  132 lines

  1. #include <stdio.h>
  2. #include <stdefs.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <io.h>
  7. #include <sys\stat.h>
  8.  
  9. #define ERROR (-1)
  10.  
  11. #define BUF_SIZE 32768
  12.  
  13. static  char    outbuf[BUF_SIZE] ;
  14. static  size_t  outsize = 0 ;
  15.  
  16.  
  17. char    *comma( long val )
  18. {
  19.     static  char    buf[32] ;
  20.             char    str[32] ;
  21.             char    *p, *q ;
  22.             int     i ;
  23.  
  24.     ltoa( val, str, 10 ) ;
  25.  
  26.     for( p=str, q=buf, i=strlen(str) ; i > 0 ; i-- )
  27.     {
  28.         *q++ = *p++ ;
  29.         if( i > 1 && ( (i-1) % 3 ) == 0 )
  30.             *q++ = ',' ;
  31.     }
  32.     *q = '\0' ;
  33.  
  34.     return buf ;
  35. }
  36. int     flush( int fd )
  37. {
  38.     return write( fd, outbuf, outsize ) ;
  39. }
  40.  
  41. /*  ファイル出力  */
  42. long    output( const char *fname, long maxline, short width )
  43. {
  44.     static  char    buf[81] ;
  45.     auto    char    tmp[64], *p ;
  46.     auto    short   max, len ;
  47.     auto    short   fd ;
  48.     auto    long    fsize ;
  49.  
  50.     /*  ファイル作成  */
  51.     if( ( fd = creat( fname, S_IREAD|S_IWRITE ) ) == ERROR )
  52.     {
  53.         printf( "can't make output file <%s>\n", fname ) ;
  54.         exit( 1 ) ;
  55.     }
  56.  
  57.     /*  バッファセット  */
  58.     memset( buf, 0x20, 255 ) ;
  59.     buf[width-2] = '\0' ;
  60.     strcat( buf, "\x0D\x0A" );
  61.  
  62.     /*  出力  */
  63.     outsize = 0 ;
  64.     len = strlen( strcpy( tmp, "\x0D\x1BT 残り " ) ) ;
  65.     p = tmp + len ;
  66.  
  67.     while( maxline > 0 )
  68.     {
  69.         strcpy( p, comma( maxline ) ) ;
  70.         strcat( p, "line" ) ;
  71.         write( 1, tmp, len + strlen( p ) ) ;
  72.  
  73.         if( maxline < 1000 ) max = maxline ;
  74.         else                 max = 1000 ;
  75.         maxline -= max ;
  76.  
  77.         while( max-- > 0 )
  78.         {
  79.             if( outsize + width > BUF_SIZE )
  80.             {
  81.                 if( flush( fd ) < outsize )
  82.                 {
  83.                     maxline = 0 ;
  84.                     break ;
  85.                 }
  86.                 outsize = 0 ;
  87.             }
  88.             memcpy( outbuf+outsize, buf, width ) ;
  89.             outsize += width ;
  90.         }
  91.     }
  92.     flush( fd ) ;
  93.  
  94.     fsize = filelength( fd ) ;
  95.     close( fd ) ;
  96.  
  97.     return fsize ;
  98. }
  99.  
  100. void    main( int argc, char *argv[] )
  101. {
  102.     long    maxline ;
  103.     short   width = 0 ;
  104.     const   char    *out_file_name = "tmp.aaa" ;
  105.     const   char    *help = "mk {行数} [文字数] --- "
  106.                             "指定文字数×行数のファイルを作ります\n" ;
  107.  
  108.     /*  パラメータ処理  */
  109.     if( argc > 1 && isdigit( *argv[1] ) )
  110.     {
  111.         maxline = atol( argv[1] ) ;
  112.         if( argc == 3 )
  113.             width = atoi( argv[2] ) ;
  114.     }
  115.     else
  116.     {
  117.         puts( help ) ;
  118.         return ;
  119.     }
  120.     if( width > 78 ) width = 78 ;
  121.     if( width < 2 )  width = 2 ;
  122.     printf( "\n %dbyte × %sline = ",
  123.                                 width, comma( maxline ) ) ;
  124.     printf( "%sbytes\n\n", comma( width*maxline ) ) ;
  125.  
  126.     /*  ファイルの作成  */
  127.     printf( "\x0D\x1BT %s の作成完了 ... サイズ %sbytes\n", out_file_name,
  128.                     comma( output( out_file_name, maxline, width ) ) ) ;
  129. }
  130.  
  131.  
  132.